package com.investment.advisor.controller;

import com.investment.advisor.dto.TagDTO;
import com.investment.advisor.entity.Tag;
import com.investment.advisor.repository.TagRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/tags")
@Api(tags = "标签管理", description = "标签的增删改查")
public class TagController {

    private final TagRepository tagRepository;

    @Autowired
    public TagController(TagRepository tagRepository) {
        this.tagRepository = tagRepository;
    }

    @GetMapping
    @ApiOperation("获取所有标签")
    public ResponseEntity<List<TagDTO>> getAllTags() {
        List<TagDTO> tags = tagRepository.findAll().stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
        return ResponseEntity.ok(tags);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID获取标签")
    public ResponseEntity<TagDTO> getTagById(@PathVariable Long id) {
        return tagRepository.findById(id)
                .map(this::convertToDTO)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @GetMapping("/category/{category}")
    @ApiOperation("根据分类获取标签")
    public ResponseEntity<List<TagDTO>> getTagsByCategory(@PathVariable String category) {
        List<TagDTO> tags = tagRepository.findByCategory(category).stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
        return ResponseEntity.ok(tags);
    }

    @PostMapping
    @ApiOperation("创建标签")
    public ResponseEntity<TagDTO> createTag(@RequestBody TagDTO tagDTO) {
        Tag tag = new Tag();
        BeanUtils.copyProperties(tagDTO, tag, "id", "createTime", "updateTime");
        Tag savedTag = tagRepository.save(tag);
        return ResponseEntity.ok(convertToDTO(savedTag));
    }

    @PutMapping("/{id}")
    @ApiOperation("更新标签")
    public ResponseEntity<TagDTO> updateTag(@PathVariable Long id, @RequestBody TagDTO tagDTO) {
        return tagRepository.findById(id)
                .map(existingTag -> {
                    BeanUtils.copyProperties(tagDTO, existingTag, "id", "createTime", "updateTime");
                    Tag updatedTag = tagRepository.save(existingTag);
                    return ResponseEntity.ok(convertToDTO(updatedTag));
                })
                .orElse(ResponseEntity.notFound().build());
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除标签")
    public ResponseEntity<Void> deleteTag(@PathVariable Long id) {
        if (tagRepository.existsById(id)) {
            tagRepository.deleteById(id);
            return ResponseEntity.noContent().build();
        }
        return ResponseEntity.notFound().build();
    }

    private TagDTO convertToDTO(Tag tag) {
        TagDTO tagDTO = new TagDTO();
        BeanUtils.copyProperties(tag, tagDTO);
        return tagDTO;
    }
} 